package cn.ccew.apk.server;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ccew.apk.model.Software;
import cn.ccew.apk.model.Version;
import cn.ccew.common.base.controller.BaseController;

@Controller
public class AndroidServerController extends BaseController {
	/**
	 * 系统版本
	 * 
	 * @throws Exception
	 */
	@RequestMapping("getsys.service")
	public void getSysVersion(HttpServletResponse response, HttpServletRequest request) throws Exception {
		Software sys = baseService.findObjByProperty("status", 1, Software.class);
		Criteria criteria = this.createCriteria(Version.class);
		criteria.add(Restrictions.eq("softId", sys.getId()));
		criteria.add(Restrictions.eq("vstate", 1));
		criteria.addOrder(Order.desc("lastModifyTime"));
		List<Version> list = criteria.list();
		for (Version v : list) {
			v.setVpath(getURL(request) + v.getVpath());
			v.setVimagepath(getURL(request) + v.getVimagepath());
		}
		if (list.size() > 0) {
			String info = JSONSerializer.toJSON(list.get(0), getConfig()).toString();
			writerSuccess(response, "调用成功", info);
		} else {
			writerFailure(response, "服务异常，请稍候重试!");
		}
	}

	/**
	 * 软件版本
	 * 
	 * @throws Exception
	 */
	@RequestMapping("getsoft.service")
	public void getSoftVersion(HttpServletResponse response,HttpServletRequest request) throws Exception {
		Software sys = baseService.findObjByProperty("status", 1, Software.class);
		Criteria criteria = this.createCriteria(Version.class);
		criteria.add(Restrictions.ne("softId", sys.getId()));
		criteria.add(Restrictions.eq("vstate", 1));
		criteria.addOrder(Order.desc("lastModifyTime"));
		List<Version> list = criteria.list();
		for (Version v : list) {
			v.setVpath(getURL(request) + v.getVpath());
			v.setVimagepath(getURL(request) + v.getVimagepath());
		}
		String info = JSONSerializer.toJSON(list, getConfig()).toString();
		writerSuccess(response, "调用成功", info);
	}

	private String getURL(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		return basePath;
	}

	public JsonConfig getConfig() {
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		config.registerJsonValueProcessor(Long.class, new LongValueProcessor());
		config.registerJsonValueProcessor(int.class, new LongValueProcessor());
		config.registerJsonValueProcessor(Timestamp.class, new TimestampValueProcessor());
		config.setExcludes(new String[] { "createTime", "lastModifyTimeString", "lastModifyTime", "createTimeStringYYYYMMdd", "createTimeString", "vstateString" });
		return config;
	}
}
